home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / Mail / sendmail.php < prev    next >
Encoding:
PHP Script  |  2005-09-26  |  5.7 KB  |  146 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Chuck Hagenbuch <chuck@horde.org>                            |
  17. // +----------------------------------------------------------------------+
  18.  
  19. /**
  20.  * Sendmail implementation of the PEAR Mail:: interface.
  21.  * @access public
  22.  * @package Mail
  23.  * @version $Revision: 1.11 $
  24.  */
  25. class Mail_sendmail extends Mail {
  26.  
  27.     /**
  28.      * The location of the sendmail or sendmail wrapper binary on the
  29.      * filesystem.
  30.      * @var string
  31.      */
  32.     var $sendmail_path = '/usr/sbin/sendmail';
  33.  
  34.     /**
  35.      * Any extra command-line parameters to pass to the sendmail or
  36.      * sendmail wrapper binary.
  37.      * @var string
  38.      */
  39.     var $sendmail_args = '';
  40.  
  41.     /**
  42.      * Constructor.
  43.      *
  44.      * Instantiates a new Mail_sendmail:: object based on the parameters
  45.      * passed in. It looks for the following parameters:
  46.      *     sendmail_path    The location of the sendmail binary on the
  47.      *                      filesystem. Defaults to '/usr/sbin/sendmail'.
  48.      *
  49.      *     sendmail_args    Any extra parameters to pass to the sendmail
  50.      *                      or sendmail wrapper binary.
  51.      *
  52.      * If a parameter is present in the $params array, it replaces the
  53.      * default.
  54.      *
  55.      * @param array $params Hash containing any parameters different from the
  56.      *              defaults.
  57.      * @access public
  58.      */
  59.     function Mail_sendmail($params)
  60.     {
  61.         if (isset($params['sendmail_path'])) $this->sendmail_path = $params['sendmail_path'];
  62.         if (isset($params['sendmail_args'])) $this->sendmail_args = $params['sendmail_args'];
  63.  
  64.         /*
  65.          * Because we need to pass message headers to the sendmail program on
  66.          * the commandline, we can't guarantee the use of the standard "\r\n"
  67.          * separator.  Instead, we use the system's native line separator.
  68.          */
  69.         $this->sep = (strpos(PHP_OS, 'WIN') === false) ? "\n" : "\r\n";
  70.     }
  71.  
  72.     /**
  73.      * Implements Mail::send() function using the sendmail
  74.      * command-line binary.
  75.      *
  76.      * @param mixed $recipients Either a comma-seperated list of recipients
  77.      *              (RFC822 compliant), or an array of recipients,
  78.      *              each RFC822 valid. This may contain recipients not
  79.      *              specified in the headers, for Bcc:, resending
  80.      *              messages, etc.
  81.      *
  82.      * @param array $headers The array of headers to send with the mail, in an
  83.      *              associative array, where the array key is the
  84.      *              header name (ie, 'Subject'), and the array value
  85.      *              is the header value (ie, 'test'). The header
  86.      *              produced from those values would be 'Subject:
  87.      *              test'.
  88.      *
  89.      * @param string $body The full text of the message body, including any
  90.      *               Mime parts, etc.
  91.      *
  92.      * @return mixed Returns true on success, or a PEAR_Error
  93.      *               containing a descriptive error message on
  94.      *               failure.
  95.      * @access public
  96.      */
  97.     function send($recipients, $headers, $body)
  98.     {
  99.         $recipients = $this->parseRecipients($recipients);
  100.         if (PEAR::isError($recipients)) {
  101.             return $recipients;
  102.         }
  103.         $recipients = escapeShellCmd(implode(' ', $recipients));
  104.  
  105.         $headerElements = $this->prepareHeaders($headers);
  106.         if (PEAR::isError($headerElements)) {
  107.             return $headerElements;
  108.         }
  109.         list($from, $text_headers) = $headerElements;
  110.  
  111.         if (!isset($from)) {
  112.             return PEAR::raiseError('No from address given.');
  113.         } elseif (strpos($from, ' ') !== false ||
  114.                   strpos($from, ';') !== false ||
  115.                   strpos($from, '&') !== false ||
  116.                   strpos($from, '`') !== false) {
  117.             return PEAR::raiseError('From address specified with dangerous characters.');
  118.         }
  119.  
  120.         $result = 0;
  121.         if (@is_file($this->sendmail_path)) {
  122.             $from = escapeShellCmd($from);
  123.             $mail = popen($this->sendmail_path . (!empty($this->sendmail_args) ? ' ' . $this->sendmail_args : '') . " -f$from -- $recipients", 'w');
  124.             fputs($mail, $text_headers);
  125.             fputs($mail, $this->sep);  // newline to end the headers section
  126.             fputs($mail, $body);
  127.             $result = pclose($mail);
  128.             if (version_compare(phpversion(), '4.2.3') == -1) {
  129.                 // With older php versions, we need to shift the
  130.                 // pclose result to get the exit code.
  131.                 $result = $result >> 8 & 0xFF;
  132.             }
  133.         } else {
  134.             return PEAR::raiseError('sendmail [' . $this->sendmail_path . '] is not a valid file');
  135.         }
  136.  
  137.         if ($result != 0) {
  138.             return PEAR::raiseError('sendmail returned error code ' . $result,
  139.                                     $result);
  140.         }
  141.  
  142.         return true;
  143.     }
  144.  
  145. }
  146.